package booklib.view;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.ButtonGroup;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.LineBorder;

import booklib.dao.BookDao;
import booklib.dao.BookTypeDao;
import booklib.model.Book;
import booklib.model.BookType;
import booklib.util.DbUtil;
import booklib.util.StringUtil;

public class BookAddInterFrame extends JInternalFrame {
	private JTextField bookNametxt;
	private JTextField authortxt;
	private final ButtonGroup buttonGroup = new ButtonGroup();
	private JTextField pricetxt;
	private DbUtil dbUtil=new DbUtil();
	private BookDao bookDao=new BookDao();
	private BookTypeDao bookTypeDao=new BookTypeDao();
	private JComboBox bookTypejcb;
	private JTextArea bookDesctxt;
	private JRadioButton manrdb ;
	private JRadioButton femalerdb;
	private JTextField bookIdtxt;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					BookAddInterFrame frame = new BookAddInterFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public BookAddInterFrame() {
		setTitle("图书添加");
		setIconifiable(true);
		setClosable(true);
		setBounds(100, 100, 614, 464);
		
		JLabel lblNewLabel = new JLabel("图书名称：");
		
		bookNametxt = new JTextField();
		bookNametxt.setColumns(10);
		
		JLabel lblNewLabel_1 = new JLabel("作者性别：");
		
		JLabel label = new JLabel("图书作者：");
		
		authortxt = new JTextField();
		authortxt.setColumns(10);
		
		manrdb = new JRadioButton("男");
		buttonGroup.add(manrdb);
		manrdb.setSelected(true);
		
		femalerdb = new JRadioButton("女");
		buttonGroup.add(femalerdb);
		
		JLabel lblNewLabel_2 = new JLabel("图书价格：");
		
		pricetxt = new JTextField();
		pricetxt.setColumns(10);
		
		JLabel label_1 = new JLabel("图书描述：");
		
		bookDesctxt = new JTextArea();
		
		JLabel label_2 = new JLabel("图书类别：");
		
		 bookTypejcb = new JComboBox();
		
		JButton btnNewButton = new JButton("添加");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				bookAddActionPerformed(e);			}
		});
		btnNewButton.setIcon(new ImageIcon(BookAddInterFrame.class.getResource("/images/play_24px_27420_easyicon.net.png")));
		
		JButton btnNewButton_1 = new JButton("重置");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				resetValueActionPerformed(e);
			}
		});
		btnNewButton_1.setIcon(new ImageIcon(BookAddInterFrame.class.getResource("/images/reset_24px_28025_easyicon.net.png")));
		
		JLabel lblid = new JLabel("图书 ID:");
		
		bookIdtxt = new JTextField();
		bookIdtxt.setColumns(10);
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(81)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(lblNewLabel_1)
							.addContainerGap())
						.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
							.addGroup(groupLayout.createSequentialGroup()
								.addComponent(lblNewLabel)
								.addContainerGap())
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addGroup(groupLayout.createSequentialGroup()
									.addGap(39)
									.addComponent(btnNewButton)
									.addPreferredGap(ComponentPlacement.RELATED, 168, Short.MAX_VALUE)
									.addComponent(btnNewButton_1)
									.addGap(128))
								.addGroup(groupLayout.createSequentialGroup()
									.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
										.addGroup(Alignment.LEADING, groupLayout.createSequentialGroup()
											.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
												.addGroup(groupLayout.createSequentialGroup()
													.addGap(81)
													.addComponent(bookNametxt, GroupLayout.PREFERRED_SIZE, 139, GroupLayout.PREFERRED_SIZE))
												.addGroup(groupLayout.createSequentialGroup()
													.addGap(89)
													.addComponent(manrdb)
													.addGap(18)
													.addComponent(femalerdb)))
											.addGap(34)
											.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
												.addGroup(groupLayout.createSequentialGroup()
													.addComponent(lblNewLabel_2)
													.addPreferredGap(ComponentPlacement.RELATED)
													.addComponent(pricetxt, 135, 135, 135))
												.addGroup(groupLayout.createSequentialGroup()
													.addComponent(label)
													.addPreferredGap(ComponentPlacement.RELATED)
													.addComponent(authortxt, GroupLayout.PREFERRED_SIZE, 134, GroupLayout.PREFERRED_SIZE))))
										.addGroup(Alignment.LEADING, groupLayout.createSequentialGroup()
											.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
												.addGroup(groupLayout.createSequentialGroup()
													.addComponent(lblid)
													.addGap(19))
												.addGroup(groupLayout.createSequentialGroup()
													.addComponent(label_1)
													.addPreferredGap(ComponentPlacement.RELATED)))
											.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
												.addGroup(groupLayout.createSequentialGroup()
													.addComponent(bookIdtxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
													.addGap(54)
													.addComponent(label_2)
													.addGap(18)
													.addComponent(bookTypejcb, GroupLayout.PREFERRED_SIZE, 155, GroupLayout.PREFERRED_SIZE))
												.addComponent(bookDesctxt, GroupLayout.DEFAULT_SIZE, 389, Short.MAX_VALUE))))
									.addContainerGap(47, Short.MAX_VALUE))))))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(26)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel)
						.addComponent(label)
						.addComponent(authortxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(bookNametxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(30)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(lblNewLabel_1)
						.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
							.addComponent(manrdb)
							.addComponent(femalerdb)
							.addComponent(lblNewLabel_2)
							.addComponent(pricetxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
					.addGap(24)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(bookIdtxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblid)
						.addComponent(label_2)
						.addComponent(bookTypejcb, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(33)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(bookDesctxt, GroupLayout.PREFERRED_SIZE, 155, GroupLayout.PREFERRED_SIZE)
						.addComponent(label_1))
					.addPreferredGap(ComponentPlacement.RELATED, 39, Short.MAX_VALUE)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnNewButton)
						.addComponent(btnNewButton_1))
					.addContainerGap())
		);
		getContentPane().setLayout(groupLayout);
		bookDesctxt.setBorder(new LineBorder(new java.awt.Color(127,157,185),1,false));
		fillBookType();
	}
	private void resetValueActionPerformed(ActionEvent e) {
		// TODO 自动生成的方法存根
		resetValue();
	}

	/**
	 * 图书添加事件处理
	 * @param e
	 */
	private void bookAddActionPerformed(ActionEvent evt) {
		// TODO 自动生成的方法存根
		String id=this.bookIdtxt.getText();
		String bookName=this.bookNametxt.getText();
		String bookDesc=this.bookDesctxt.getText();
		String author=this.authortxt.getText();
		String price =this.pricetxt.getText();
		if(StringUtil.isEmpty(id)){
			JOptionPane.showMessageDialog(null,"图书ID不能为空！");
			return;
		}
		if(StringUtil.isEmpty(bookName)){
			JOptionPane.showMessageDialog(null,"图书名称不能为空！");
			return;
		}
		if(StringUtil.isEmpty(author)){
			JOptionPane.showMessageDialog(null,"图书作者不能为空！");
			return;
		}
		if(StringUtil.isEmpty(price)){
			JOptionPane.showMessageDialog(null,"图书价格不能为空！");
			return;
		}
		String sex="";
		if(manrdb.isSelected()){
			sex="男";
			
		}
		else if(femalerdb.isSelected()){
			sex="女";
			
		}
		BookType bookType=(BookType) bookTypejcb.getSelectedItem();
		int bookTypeId=bookType.getId();
		Book book=new Book(Integer.parseInt(id),bookName, author, sex, bookTypeId,Float.parseFloat(price), bookDesc);
		Connection con=null;
		try{
			con=dbUtil.getCon();
			int addNum=bookDao.add(con, book,bookType);
			if(addNum==1){
				JOptionPane.showMessageDialog(null,"图书添加成功！");
				resetValue();
			}
			else{
				JOptionPane.showMessageDialog(null,"图书添加失败！");
			}
		}
		catch(Exception e){
			e.printStackTrace();
			JOptionPane.showMessageDialog(null,"图书添加失败！");
		}
		finally{
			try {
				dbUtil.closeCon(con);
			} catch (Exception e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
			
		}
	}
	/**
	 * 重置表单
	 */
	private void resetValue() {
		// TODO 自动生成的方法存根
		this.bookIdtxt.setText("");
		this.bookNametxt.setText("");
		this.authortxt.setText("");
		this.pricetxt.setText("");
		this.bookDesctxt.setText("");
		this.manrdb.setSelected(true);
		if(this.bookTypejcb.getItemCount()>0){
			this.bookTypejcb.setSelectedIndex(0);
		}
	}

	/**
	 * 初始化图书类别下拉框
	 */
	private void fillBookType(){
		Connection con=null;
		BookType bookType =null;
		try{
			con=dbUtil.getCon();
			ResultSet rs=bookTypeDao.list(con,new BookType());
			while(rs.next()){
				bookType=new BookType();
				bookType.setId(rs.getInt("id"));
				bookType.setBookTypeName(rs.getString("bookTypeName"));
				this.bookTypejcb.addItem(bookType);
			}
		}
		catch(Exception e){
			e.printStackTrace();
		}
		finally{
			try {
				con.close();
			} catch (SQLException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
		}
	}
}
